package com.chaofan.handler;

import com.chaofan.domain.R;
import com.chaofan.domain.exception.ConditionException;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ValidationException;

/**
 * @author 李超凡
 * @since 2022/1/12 22:55
 */
@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class GlobalExceptionHandler {

    @ExceptionHandler(ConditionException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public R<String> handler(ConditionException e) {
        return R.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(ValidationException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public R<String> handler(ValidationException e) {
        return R.fail("400", e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public R<String> handler(Exception e) {
        return R.fail("400", e.getMessage());
    }
}
